package com.catsic.web.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.catsic.base.controller.BaseController;
import com.catsic.core.menu.service.MenuService;
import com.catsic.core.form.MenuForm;

@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController {
	
	@Autowired
	private MenuService menuService;
	
	/**
	 * create:添加
	 * @author yangyd
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/create" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> create(MenuForm form){
		form = menuService.add(form);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * update:更新
	 * @author yangyd
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/update" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> update(MenuForm form){
		form = menuService.update(form);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * cancel:注销
	 * @author yangyd
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/cancel")
	@ResponseBody
	public Map<String,Object> cancel(String id){
		menuService.cancel(id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * delete:删除
	 * @author yangyd
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String,Object> delete(String id){
		menuService.delete(id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}

    /**
     * 激活
	 * @author yangyd
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/active")
	@ResponseBody
	public Map<String,Object> active(String id){
		menuService.active(id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
     * 通过父节点ID获取子节点
	 * @author yangyd
	 * @param condition
	 * @return
	 */
	@RequestMapping(value = "/query" )
	@ResponseBody
	public Map<String,Object> query(String id){
		List<MenuForm> list = menuService.query(id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, list);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
}
